package cn.ctvonline.android.kaopu.support;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.view.WindowManager;

import cn.ctvonline.android.kaopu.widget.layout.SwipeLayout;

/**
 * Created by dujian on 15/12/22.
 */
public class SwipeHelper {
    private static final ThreadLocal<SwipeHelper> threadLocal = new ThreadLocal<>();
    private int screenWidth;

    private SwipeHelper() {

    }

    public static SwipeHelper getInstance() {
        if (null == threadLocal.get()) {
            SwipeHelper instance = new SwipeHelper();
            threadLocal.set(instance);
        }
        return threadLocal.get();
    }

    public void replaceSwipe(Activity activity, SwipeLayout.OnCloseToBorderListener onCloseToBorderListener) {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager manager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        manager.getDefaultDisplay().getMetrics(metrics);
        screenWidth = metrics.widthPixels;
        SwipeLayout swipeLayout = new SwipeLayout(activity);
        swipeLayout.setSwipeEnabled(true);
        swipeLayout.setSwipeAnyWhere(true);
        swipeLayout.setClickable(true);
        swipeLayout.setOnCloseToBorderListener(onCloseToBorderListener);
        final ViewGroup root = (ViewGroup) activity.getWindow().getDecorView();
        ViewGroup content = (ViewGroup) root.getChildAt(0);
        ViewGroup.LayoutParams params = content.getLayoutParams();
        ViewGroup.LayoutParams params2 = new ViewGroup.LayoutParams(-1, -1);
        root.removeView(content);
        swipeLayout.addView(content, params2);
        root.addView(swipeLayout, params);
    }
}
